<%#
 Copyright 2013-2025 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
package <%= entityAbsolutePackage %>.repository;

import <%= entityAbsolutePackage %>.domain.<%= persistClass %>;
<%_ if (jpaMetamodelFiltering) { _%>
import <%= entityAbsolutePackage %>.domain.criteria.<%= entityClass %>Criteria;
<%_ } _%>
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.data.relational.core.query.Criteria;
import org.springframework.stereotype.Repository;
import org.springframework.data.domain.Pageable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
<%_ if (primaryKey.typeUUID) { _%>

import java.util.UUID;
<%_ } _%>

/**
 * <%= springDataDescription %> repository for the <%= persistClass %> entity.
 */
@SuppressWarnings("unused")
@Repository
public interface <%= entityClass %>Repository extends ReactiveCrudRepository<<%= persistClass %>, <%= primaryKey.type %>>, <%= entityClass %>RepositoryInternal {

<%_ if (!paginationNo) { _%>
    Flux<<%= persistClass %>> findAllBy(Pageable pageable);
<%_ } _%>
<%_ if (implementsEagerLoadApis) { _%>

    @Override
    Mono<<%= persistClass %>> findOneWithEagerRelationships(<%= primaryKey.type %> id);

    @Override
    Flux<<%= persistClass %>> findAllWithEagerRelationships();

    @Override
    Flux<<%= persistClass %>> findAllWithEagerRelationships(Pageable page);

<%_ } _%>
<%_ for (const relationship of relationships) {
    let ownerSide = relationship.ownerSide; _%>
  <%_ if (relationship.relationshipManyToOne || (relationship.relationshipOneToOne && ownerSide)) { _%>

    @Query("SELECT * FROM <%= entityTableName %> entity WHERE entity.<%= relationship.joinColumnNames[0] %> = :id")
    Flux<<%= persistClass %>> findBy<%= relationship.relationshipNameCapitalized %>(<%= primaryKey.type %> id);

    @Query("SELECT * FROM <%= entityTableName %> entity WHERE entity.<%= relationship.joinColumnNames[0] %> IS NULL")
    Flux<<%= persistClass %>> findAllWhere<%= relationship.relationshipNameCapitalized %>IsNull();
  <%_ } else if (relationship.shouldWriteJoinTable) { _%>

    @Query("SELECT entity.* FROM <%= entityTableName %> entity JOIN <%= relationship.joinTable.name %> joinTable ON entity.<%= primaryKey.fields[0].columnName %> = joinTable.<%= relationship.joinColumnNames[0] %> WHERE joinTable.<%= relationship.joinColumnNames[0] %> = :id")
    Flux<<%= persistClass %>> findBy<%= relationship.relationshipNameCapitalized %>(<%= primaryKey.type %> id);
  <%_ } else if (relationship.relationshipOneToOne && !ownerSide) { _%>

    @Query("SELECT * FROM <%= entityTableName %> entity WHERE entity.<%= primaryKey.fields[0].columnName %> not in (select <%= relationship.joinColumnNames[0]%> from <%= relationship.otherEntity.entityTableName %>)")
    Flux<<%= persistClass %>> findAllWhere<%= relationship.relationshipNameCapitalized %>IsNull();
  <%_ } _%>
<%_ } _%>

    @Override
    <S extends <%= persistClass %>> Mono<S> save(S entity);

    @Override
    Flux<<%= persistClass %>> findAll();

    @Override
    Mono<<%= persistClass %>> findById(<%= primaryKey.type %> id);

    @Override
    Mono<Void> deleteById(<%= primaryKey.type %> id);
}

interface <%= entityClass %>RepositoryInternal {
    <S extends <%= persistClass %>> Mono<S> save(S entity);

    Flux<<%= persistClass %>> findAllBy(Pageable pageable);

    Flux<<%= persistClass %>> findAll();

    Mono<<%= persistClass %>> findById(<%= primaryKey.type %> id);
    // this is not supported at the moment because of https://github.com/jhipster/generator-jhipster/issues/18269
    // Flux<<%= persistClass %>> findAllBy(Pageable pageable, Criteria criteria);
<%_ if (jpaMetamodelFiltering) { _%>
    Flux<<%= persistClass %>> findByCriteria(<%= entityClass %>Criteria criteria, Pageable pageable);

    Mono<Long> countByCriteria(<%= entityClass %>Criteria criteria);
<%_ } _%>
<%_ if (implementsEagerLoadApis) { _%>

    Mono<<%= persistClass %>> findOneWithEagerRelationships(<%= primaryKey.type %> id);

    Flux<<%= persistClass %>> findAllWithEagerRelationships();

    Flux<<%= persistClass %>> findAllWithEagerRelationships(Pageable page);

    Mono<Void> deleteById(<%= primaryKey.type %> id);
<%_ } _%>
}
